(function (a2dmah5) {
    a2dmah5.addExpandPage(0, function (get) {
        const Common = get('UtilsCommon', 'Common');

        return {
            expandInit() {
                this.saveEntityData2 = this.getProperty('saveEntityData2');
                this.saveEntityData2.isVisible = !this.pageData.PurchaseOrderId || this.pageData.isCopy === 'true';
                this.detailsProperty = this.getProperty('Details');
                this.detailsProperty.setDataLoad = this.detailsChange.bind(this);

                this.isDataLoad = !this.pageData.PurchaseOrderId;

                const entityEdit2 = this.getProperty('entityEdit2');
                entityEdit2.properties.forEach(p => {
                    if (p.name === 'ProductAmount') { this.productAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'FreightAmount') { this.freightAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'OtherAmount') { this.otherAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'DiscountAmount') { this.discountAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'TotalAmount') { this.totalAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                });
            },
            expandSetEntityData({ entityData }) {
                entityData.Status = 0;
                let blExists = false;
                const materialProductIdList = [];

                for (let i = 0; i < entityData.Details.length; i++) {
                    const id = entityData.Details[i].MaterialProductId;
                    if (materialProductIdList.includes(id)) { blExists = true; break; }
                    else materialProductIdList.push(id);
                }

                if (blExists) {
                    this.alert('原料商品有重复添加');
                    return false;
                }

                return entityData;
            },
            expandDataLoad({ data }) {
                if ((data.Status == 0 || data.Status == 1) && Common.isEquals(data.CreateUser, this.loginUser.UserId, true)) this.saveEntityData2.setIsVisible(true);
                setTimeout(() => this.isDataLoad = true, 100);
            },
            detailsChange(dataList) {
                let productAmount = 0;
                dataList = dataList || [];
                if (dataList.length === 0 || !this.isDataLoad) return;

                dataList.forEach(d => {
                    productAmount += Common.enlarge(d.Amount);
                });

                productAmount = productAmount / 100;

                this.productAmountProperty.setValue(productAmount);
                this.setTotalAmount(null, null, null, productAmount);
            },
            setTotalAmount(value, property, view, productAmount) {
                if (property && !property.isChanged) return;
                if (productAmount === undefined) productAmount = Common.enlarge(this.productAmountProperty.getValue());
                else productAmount = Common.enlarge(productAmount);

                let freightAmount = Common.enlarge(this.freightAmountProperty.getValue());
                let otherAmount = Common.enlarge(this.otherAmountProperty.getValue());
                let discountAmount = Common.enlarge(this.discountAmountProperty.getValue());

                let totalAmount = productAmount + freightAmount + otherAmount - discountAmount;
                totalAmount = totalAmount / 100;
                this.totalAmountProperty.setValue(totalAmount);
            },
            setRowDataProperties({ view }) {
                view.properties.forEach(p => {
                    if (p.name === 'MaterialProductId') p.valueChange = this.materialProductIdChange.bind(this);
                    else if (p.name === 'Price' || p.name === 'Num' || p.name === 'DiscountAmount') p.valueChange = this.inputValueChange.bind(this);
                });
            },
            materialProductIdChange(value, selectData, property, view) {
                if (!property.isChanged) return;
                if (selectData) this.computeAmount(view, selectData.InPrice);
                property.isChanged = false;
            },
            inputValueChange(value, property, view) {
                if (!property.isChanged) return;
                this.computeAmount(view);
                property.isChanged = false;
            },
            computeAmount(view, price2) {
                let price = price2 || 0;
                let num = 0;
                let discountAmount = 0;
                let amount = 0;
                let amountProperty;
                view.properties.forEach(p => {
                    if (p.name === 'Price') {
                        if (price2 !== undefined) {
                            p.setValue(price2);
                            p.data.Price = price2;
                        }
                        else price = Common.getFloat(p.data.Price);
                    }
                    else if (p.name === 'Num') num = Common.getFloat(p.data.Num);
                    else if (p.name === 'DiscountAmount') discountAmount = Common.enlarge(p.data.DiscountAmount);
                    else if (p.name === 'Amount') amountProperty = p;
                });

                let a = Common.enlarge(price * num, 2);
                amount = (a - discountAmount) / 100;
                amountProperty.setValue(amount);
                amountProperty.data.Amount = amount;
                this.detailsChange(this.detailsProperty.getValue());
            }
        }
    });

})(window.a2dmah5);